package routers

import (
	"fmt"
	"github.com/fantomo/go-iching/config"
	"github.com/fantomo/go-iching/middleware"
	"github.com/fantomo/go-iching/routers/api"
	"github.com/gin-gonic/gin"
	"time"
)

func logFormat(param gin.LogFormatterParams) string {
	// your custom format
	return fmt.Sprintf("%s - [%s] \"%s %s %s %s %d %s \"%s\" %s\"\n",
		param.ClientIP,
		param.TimeStamp.Format(time.RFC3339Nano),
		param.Method,
		param.Path,
		param.Request.RemoteAddr,
		param.Request.Proto,
		param.StatusCode,
		param.Latency,
		param.Request.UserAgent(),
		param.ErrorMessage,
	)
}

// SetReleaseMode 设置生产环境
func SetReleaseMode() bool {
	if config.GetConf().Runtime.ENV == "production" {
		gin.SetMode(gin.ReleaseMode)
		return true
	} else {
		return false
	}
}

// InitRouter
//
//	@host      localhost
//	@BasePath  /admin
func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(middleware.Cors()).Use(gin.LoggerWithFormatter(logFormat))

	gin.SetMode(gin.ReleaseMode)
	api.RegisterIchingRouter(r)
	return r
}
